package com.cc.peripheraldemo

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.gpsdk.demo.gpsdkdemo.GMainActivity
import com.jyt.peripheral.print.PrintInfo
import com.jyt.peripheral.print.PrintManager
import com.jyt.peripheral.toast
import com.jyt.peripheral.weight.WeightManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        WeightManager.getInstance(this.application)
        //标签打印
        btn_tag_print_test?.setOnClickListener {
            PrintManager.getInstance(this).toTagPrint(tranPrintInfo()){
                toast("${if(it){"打印成功"}else{"打印失败"}}")
            }
        }
        btn_tag_set?.setOnClickListener {
            PrintManager.getInstance(this).usbPortSelect()
        }
        //电子秤端口设置
        btn_weight_set?.setOnClickListener {
            WeightManager.getInstance().goWeightSetting()
            btn_weight_set?.postDelayed({
                btn_weight_test_show?.text = "一直看到此信息说明端口或波特率设置错误"
            }, 100)
        }
        //电子秤连接监听
        WeightManager.getInstance().toAddWeightListener { b, vlaue ->
            btn_weight_test_show?.text = vlaue
            tv_weight_config?.text = "当前配置：${WeightManager.path ?: "-"}:${WeightManager.baudrate ?: "-"}"
        }
        //一体机小票打印测试（小票打印机仅限一体打印机,usb的请查看佳博demo）
        btn_ticket_print_test?.setOnClickListener {
            PrintManager.getInstance(this).toTicketPrintTest{
                toast("${if(it){"打印成功"}else{"打印失败"}}")
            }
        }
        //一体机钱箱开启（小票打印机仅限一体打印机,usb的请查看佳博demo）
        btn_open_drawer?.setOnClickListener {
            PrintManager.getInstance(this).openDrawer()
        }
        //佳博打印测试
        btn_ticket_jb_print_test?.setOnClickListener {
            val intent = Intent()
            intent.setClass(this,GMainActivity::class.java)
            startActivity(intent)
        }
    }


    private fun tranPrintInfo(): PrintInfo? {
        var printInfo: PrintInfo? = null
        val info = ArrayList<String>()
        info.add("牛大吉")
        info.add("QRCODE===标签测试打印0001")
        info.add("001")
        info.add("商品名称:测试商品名称")
        info.add("商品规格:份")
        info.add( "2020 9 10 12:21:22" )
        info.add("配送")

        printInfo = PrintInfo()
        printInfo.printdata = info
        return printInfo
    }

    override fun onDestroy() {
        PrintManager.getInstance(this).closeAll()
        WeightManager.getInstance(this.application).closeSerialPort()
        super.onDestroy()
    }
}